package cn.iocoder.yudao.module.enterprise.service.report;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.enterprise.controller.admin.report.vo.ReportReductionCreateReqVO;
import cn.iocoder.yudao.module.enterprise.controller.admin.report.vo.ReportReductionPageReqVO;
import cn.iocoder.yudao.module.enterprise.convert.report.ReportReductionConvert;
import cn.iocoder.yudao.module.enterprise.dal.dataobject.report.ReportReductionDO;
import cn.iocoder.yudao.module.enterprise.dal.mysql.report.ReportReductionMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.enterprise.enums.ErrorCodeConstants.REPORT_REDUCTION_NOT_EXISTS;

/**
 * 核算报告和减排信息关联 Service 实现类
 *
 * @author StevenShen
 */
@Service
@Validated
public class ReportReductionServiceImpl implements ReportReductionService {

    @Resource
    private ReportReductionMapper reportReductionMapper;

    @Override
    public Long createReportReduction(ReportReductionCreateReqVO createReqVO) {
        // 插入
        ReportReductionDO reportReduction = ReportReductionConvert.INSTANCE.convert(createReqVO);
        reportReductionMapper.insert(reportReduction);
        // 返回
        return reportReduction.getId();
    }

    @Override
    public void deleteReportReduction(Long id) {
        // 校验存在
        validateReportReductionExists(id);
        // 删除
        reportReductionMapper.deleteById(id);
    }

    private void validateReportReductionExists(Long id) {
        if (reportReductionMapper.selectById(id) == null) {
            throw exception(REPORT_REDUCTION_NOT_EXISTS);
        }
    }

    @Override
    public PageResult<ReportReductionDO> getReportReductionPage(ReportReductionPageReqVO pageReqVO) {
        return reportReductionMapper.selectPage(pageReqVO);
    }

    @Override
    public List<ReportReductionDO> getReportReductionListByReportId(Long reportId) {
        return reportReductionMapper.selectListByReportId(reportId);
    }

}
